package com.scu.bank.service.impl;

import com.scu.bank.core.cache.SystemCache;
import com.scu.bank.core.exception.ServiceException;
import com.scu.bank.core.jwt.JwtInfo;
import com.scu.bank.model.base.BaseServiceImpl;
import com.scu.bank.model.dao.UserInfoDao;
import com.scu.bank.model.example.UserInfoExample;
import com.scu.bank.model.pojo.UserInfo;
import com.scu.bank.model.pojo.UserRole;
import com.scu.bank.model.vo.Menu;
import com.scu.bank.service.JwtTokenService;
import com.scu.bank.service.LoginService;
import com.scu.bank.service.UserRoleService;
import com.scu.bank.util.*;
import com.scu.bank.util.json.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class LoginServiceImpl extends BaseServiceImpl<UserInfo,UserInfoDao> implements LoginService{

    @Autowired
    @Qualifier("userCacheService")
    private SystemCache userCacheService;
    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private JwtTokenService tokenService;
    @Autowired
    private UserRoleService userRoleService;
    @Value("${view.index}")
    private String indexView;
    @Value("${view.manager}")
    private String managerView;
    @Value("${view.personalWork}")
    private String personalWorkView;
    @Value("${view.statistics}")
    private String statisticsView;

    @Override
    public String login(String userName, String cipher) {
        UserInfo userInfo = getByUserLoginName(userName);
//        if (!cipher.equals(DigestUtils.md5DigestAsHex(userInfo.getCipher().getBytes()))){
        if (!cipher.equals(userInfo.getCipher())){
            throw new ServiceException("密码不正确!");
        }

        // 将用户信息放入缓存
        userCacheService.save(userInfo.getId(),userInfo);

        // 封装签名信息
        JwtInfo jwtInfo = new JwtInfo();
        jwtInfo.setUid(userInfo.getId());
        jwtInfo.setProfile(userInfo.getProfilePhoto());
        jwtInfo.setUserName(userInfo.getUserName());

        return tokenService.generatorToken(jwtInfo);
    }

    @Override
    public List<Menu> getMenu() {
        List<Menu> menus = new ArrayList<>();
        UserInfo currentUser = WebUtil.getCurrentUser();

        // 先拿到全部菜单配置
        Menu indexMenu = JsonUtils.jsonToPojo(indexView,Menu.class);
        Menu managerMenu = JsonUtils.jsonToPojo(managerView,Menu.class);
        Menu personalWorkMenu = JsonUtils.jsonToPojo(personalWorkView,Menu.class);
        Menu statisticsMenu = JsonUtils.jsonToPojo(statisticsView,Menu.class);

        menus.add(indexMenu);
        // 系统管理员有全部菜单
        if (currentUser.getIsAdmin()){
            menus.add(managerMenu);
            menus.add(personalWorkMenu);
            menus.add(statisticsMenu);
            return menus;
        }

        UserRole userRole = userRoleService.getById(currentUser.getUserRole());
        int role = userRole == null ? 0 : userRole.getPermission();
        // 科研人员
        if (UserRoleVerifyUtil.isScientificResearcher(role)){
            menus.add(personalWorkMenu);
        }
        // 学校领导
        if (UserRoleVerifyUtil.isSchoolLeader(role)){
            menus.add(statisticsMenu);
        }
        return menus;
    }

    private UserInfo getByUserLoginName(String loginName) {
        UserInfoExample example = new UserInfoExample();
        example.createCriteria()
                .andIsDelEqualTo(false)
                .andLoginNameEqualTo(loginName);
        List<UserInfo> userInfos = userInfoDao.selectByExample(example);
        if (ListUtils.isEmpty(userInfos) || userInfos.size() > 1){
            throw new ServiceException("账户名不存在!");
        }
        return userInfos.get(0);
    }

    @Override
    public UserInfoDao getDao() {
        return userInfoDao;
    }


}
